package com.longdaji.decoration.ui.goodscategory;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.GoodsCategory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class GoodsCategoryAdapter extends RecyclerView.Adapter<GoodsCategoryAdapter.ViewHolder> {

    private List<GoodsCategory.SecondCategory> mCategoryList = new ArrayList<>();
    private ItemClickListener onItemClickListener;
    private int curPos = 0;

    public GoodsCategoryAdapter(List<GoodsCategory.SecondCategory> categoryList) {
        if (categoryList != null) {
            this.mCategoryList = categoryList;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category_title, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.title.setText(mCategoryList.get(position).getCategoryName());
        if (curPos == position) {
            holder.title.setBackgroundResource(R.drawable.white_solid_rect);

        } else {
            holder.title.setBackgroundResource(R.drawable.grey_solid_rect);

        }
        holder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onClick(position);
                curPos = position;
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mCategoryList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title;


        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_category_title);

        }
    }

    public interface ItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(ItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
